package com.pig4cloud.pig.module.controller.devices;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.devices.EstateRobot;
import com.pig4cloud.pig.module.api.entity.estate.Estate;
import com.pig4cloud.pig.module.api.entity.property.Property;
import com.pig4cloud.pig.module.service.devices.IEstateRobotService;
import com.pig4cloud.pig.module.service.estate.IEstateService;
import com.pig4cloud.pig.module.service.property.IPropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 小区的智能机器人终端Controller
 *
 * @author ruoyi
 * @date 2023-09-07
 */
@Validated
@Api(value = "小区的智能机器人终端控制器", tags = {"小区的智能机器人终端管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/devices/robot")
public class EstateRobotController {

    private final IEstateRobotService estateRobotService;
    private final IEstateService estateService;
    private final IPropertyService propertyService;

    /**
     * 查询小区的智能机器人终端列表
     */
    @ApiOperation("查询小区的智能机器人终端列表")
    @PreAuthorize("@pms.hasPermission('module:robot:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateRobot> list(@Validated EstateRobot estateRobot) {
        QueryWrapper<EstateRobot> qw = new QueryWrapper<>();

        TableDataInfo<EstateRobot> td = PageUtils.buildDataInfo(estateRobotService.page(PageUtils.buildPage(),qw));
        List<EstateRobot> list = td.getData();
        return td;
    }

    /**
     * 导出小区的智能机器人终端列表
     */
    @ApiOperation("导出小区的智能机器人终端列表")
    @PreAuthorize("@pms.hasPermission('module:robot:export')")
    @SysLog("小区的智能机器人终端")
    @GetMapping("/export")
    public List export(@Validated EstateRobot estateRobot) {
        List<EstateRobot> list = estateRobotService.list();
        // ExcelUtil<EstateRobot> util = new // ExcelUtil<EstateRobot>(EstateRobot.class);
        return  list;// util.exportExcel(list, "小区的智能机器人终端");
    }

    /**
     * 获取小区的智能机器人终端详细信息
     */
    @ApiOperation("获取小区的智能机器人终端详细信息")
    @PreAuthorize("@pms.hasPermission('module:robot:query')")
    @GetMapping("/{id}")
    public R<EstateRobot> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateRobot estateRobot = estateRobotService.getById(id);
        return R.ok(estateRobot);
    }

    /**
     * 新增小区的智能机器人终端
     */
    @ApiOperation("新增小区的智能机器人终端")
    @PreAuthorize("@pms.hasPermission('module:robot:add')")
    @SysLog("小区的智能机器人终端")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody EstateRobot estateRobot) {
        if (estateRobot.getEstateId()!=null){
            Estate estate = estateService.getById(estateRobot.getEstateId());
            if (estate!=null){
                estateRobot.setEstateName(estate.getEstateName());
            }
        }
        if (estateRobot.getPropertyId()!=null){
            Property property = propertyService.getById(estateRobot.getPropertyId());
            if (property!=null){
                estateRobot.setPropertyName(property.getPropertyName());
            }
        }
        return R.okByRow(estateRobotService.save(estateRobot) ? 1 : 0);
    }

    /**
     * 修改小区的智能机器人终端
     */
    @ApiOperation("修改小区的智能机器人终端")
    @PreAuthorize("@pms.hasPermission('module:robot:edit')")
    @SysLog("小区的智能机器人终端")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody EstateRobot estateRobot) {
        return R.okByRow(estateRobotService.updateById(estateRobot) ? 1 : 0);
    }

    /**
     * 删除小区的智能机器人终端
     */
    @ApiOperation("删除小区的智能机器人终端")
    @PreAuthorize("@pms.hasPermission('module:robot:remove')")
    @SysLog("小区的智能机器人终端")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(estateRobotService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
